/**
 \* Created with IntelliJ IDEA.
 \* @author: 彭诗杰
 \* @time: 2019/9/6 16:09
 \* Description: 大屏展示 state
 \*/

import API from "../../api/api_screen"

const state = {
  factorAlarms: [],
  deviceAlarms: [],
  // 综合情况state
  comprehensive: {},
  factorExceed: {},
  deviceFaulty: {},
};

const getters = {
  factorAlarms: state => {
    return state.factorAlarms;
  },
  deviceAlarms: state => {
    return state.deviceAlarms;
  },
  factorExceed: state => {
    return state.factorExceed;
  },
  deviceFaulty: state => {
    return state.deviceFaulty;
  },
  comprehensive: state => {
    return state.comprehensive;
  }
};

const actions = {

  initFactorAlarmsAct({commit, state}, params) {
    return new Promise((resolve, reject) => {
      API.getFactorAlarms(params).then(result => {
        console.log('setFactorAlarms:', result.factorAlarms);
        commit('setFactorAlarms', result.factorAlarms);
        resolve(result);
      }, err => {
        reject(err);
      }).catch(error => {
        reject(error);
      })
    })
  },

  initDeviceAlarmsAct({commit, state}, params) {
    return new Promise(((resolve, reject) => {
      API.getDeviceAlarms(params).then(result => {
        commit('setDeviceAlarms', result.deviceAlarms);
        resolve(result);
      }, err => {
        reject(err);
      }).catch(error => {
        reject(error);
      })
    }))
  },

  initFactorExceedAct({commit, state}) {
    return new Promise((resolve, reject) => {
      API.getFactorExceed().then(result => {
        commit('setFactorExceed', result.factorExceed);
        resolve(result);
      }).catch(error => {
        reject(error);
      })
    })
  },

  initDeviceFaultyAct({commit, state}) {
    return new Promise(((resolve, reject) => {
      API.getDeviceFaulty().then(result => {
        commit('setDeviceFaulty', result.deviceFaulty);
        resolve(result);
      }).catch(error => {
        reject(error);
      })
    }))
  },

  initComprehensiveAct({commit, state}, params) {
    return new Promise((resolve, reject) => {
      API.getComprehensiveData(params).then(result => {
        commit('setComprehensive', result.comprehensive);
        resolve(result.comprehensive);
      }, err => {
        reject(err);
      }).catch(error => {
        reject(error)
      })
    })
  }
};

const mutations = {
  setFactorAlarms(state, entity) {
    state.factorAlarms = entity;
  },
  setDeviceAlarms(state, entity) {
    state.deviceAlarms = entity;
  },
  setComprehensive(state, entity) {
    state.comprehensive = entity;
  },
  setFactorExceed(state, entity) {
    state.factorExceed = entity;
  },
  setDeviceFaulty(state, entity) {
    state.deviceFaulty = entity;
  }
};

export default {
  state,
  getters,
  actions,
  mutations
}

